﻿using AutoMapper;
using MediatR;
using Sale.Api.Application.工单.Command.OpenFare;
using Sale.Domain.工单;
using Sale.ErrorCode;
using Sale.Infrastructure.Implement;

namespace Sale.Api.Application.工单.Handler.OpenFare
{
    public class AddOpenFareHandler : IRequestHandler<AddOpenFareCommand, ApiResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ILogger<AddOpenFareHandler> logger;
        private readonly DatabaseContext db;

        public AddOpenFareHandler(IMapper mapper,ILogger<AddOpenFareHandler> logger,DatabaseContext db)
        {
            this.mapper = mapper;
            this.logger = logger;
            this.db = db;
        }
        public async Task<ApiResult<int>> Handle(AddOpenFareCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result=new ApiResult<int>();
            var openFare = mapper.Map<OpenFareModel>(request);
            int r=await db.SqlSugar.Insertable(openFare).ExecuteCommandAsync();
            if (r > 0)
            {
                result.Msg = "开票管理添加成功";
                result.Data = r;
                result.Code = ApiEnum.OK;
                logger.LogInformation(result.Msg);
            }
            else
            {
                result.Msg = "开票管理添加失败";
                result.Data = r;
                result.Code = ApiEnum.Fail;
                logger.LogError(result.Msg);
            }
            return await Task.FromResult(result);
        }
    }
}
